package com.zhiyou.servlet;

import com.zhiyou.model.Rent;
import com.zhiyou.model.User;
import com.zhiyou.service.UserListService;
import com.zhiyou.service.impl.UserLisetServiceImp;
import com.zhiyou.util.PageUtil;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;

public class UserListServlet extends HttpServlet {
    private UserListService uLS = new UserLisetServiceImp();
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

    }

    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        resp.setContentType("text/html;charset=utf-8");
        //接收参数
        String pageNoStr = req.getParameter("pageNo");
        System.out.println("pageNoStr="+pageNoStr);
        //第pageNo页
        int pageNo = Integer.parseInt(pageNoStr);
        System.out.println("pageNo="+pageNo);

        //接受模糊查询关键字
        String keyword = req.getParameter("keyword");

        //创建HashMap对象封装数据
        HashMap<String, String> keywordMap = new HashMap<>();
        keywordMap.put("keyword",keyword);

        //记录信息的数量
        int total = uLS.getUserListNum(keywordMap);
        //创建分页工具对象
        PageUtil pageUtil = new PageUtil(pageNo, total);
        //获取全部信息
        List<User> users =uLS.getUser(pageNo,pageUtil.getPageSize(),keywordMap);
        req.getSession().setAttribute("users",users);
        req.getSession().setAttribute("keywordMap",keywordMap);
        req.getSession().setAttribute("pageUtil",pageUtil);
        resp.sendRedirect(req.getContextPath()+"/view/user/list.jsp");
    }
}
